<?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$config = array(
        'templates.path' => __DIR__
);
$app = new \Slim\Slim($config);

$app->get('/', function  () use( $app)
{
    $app->render('help.html');
});

$app->get('/result(/:date)', 
        function  ($date = null) use( $app)
        {
            $strMeta = "<meta http-equiv='content-type' content='text/html; charset=UTF-8'>";
            
            if ($date == null) {
                $text = '請輸入年月日(ex: result/' . date("Ymd") . ')';
                echo $strMeta .
                         "<script language='javascript'>window.alert('$text');</script>";
            } else {
                if (file_exists("result/$date.csv")) {
                    $text = file_get_contents("result/$date.csv");
                    $content = "";
                    foreach (explode("\n", $text) as $line) {
                        if (empty($line)) {
                            continue;
                        }
                        $row = "";
                        foreach (explode(", ", $line) as $item) {
                            $row = $row . "<td>" . $item . "</td>";
                        }
                        $content = $content . "<tr>" . $row . "</tr>";
                    }
                    $html = "<html><head>
            <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><table border=\"1\">" .
                             $content . "</table></html>";
                    echo $html;
                } else {
                    $text = "尚無中獎名單";
                    
                    echo $strMeta .
                             "<script language='javascript'>window.alert('$text');</script>";
                }
            }
        });

$app->get('/play', 
        function  () use( $app)
        {
            $app->render('play.html');
        });

$app->get('/api', 
        function  ()
        {
            require ('models/Result.php');
            echo Result::get();
        });

$app->get('/prize(/:method(/:id(/:amount)))', 
        function  ($method = 'get', $id = null, $amount = null)
        {
            require ('models/Result.php');
            if ($method == 'save')
                echo Result::savePrize($id, $amount);
            else
                echo Result::getPrize();
        });

$app->get('/admin(/:name)', 
        function  ($name = null) use( $app)
        {
            if (! isset($_SERVER['PHP_AUTH_USER']) ||
                     ! isset($_SERVER['PHP_AUTH_PW']) ||
                     $_SERVER['PHP_AUTH_USER'] !== 'demo' ||
                     $_SERVER['PHP_AUTH_PW'] !== 'demo') {
                header("WWW-Authenticate: Basic realm=\"Secure Page\"");
                header("HTTP\ 1.0 401 Unauthorized");
                echo 'No soup for you';
                exit();
            }
            
            // echo "Hello, $name";
            $app->render('admin.html');
        });

$app->get('/prizeName', 
        function  ()
        {
            require ('models/Result.php');
            header('Content-Type: application/json; charset=utf-8');
            echo Result::getPrizeName();
        });

$app->post('/postPrizeName', 
        function  () use( $app)
        {
            $req = $app->request;
            $data = json_decode($app->request->getBody());
            require ('models/Result.php');
            Result::savePrizeName($data);
        });

$app->get('/winPoss',
        function  ()
        {
            require ('models/Result.php');
            header('Content-Type: application/json; charset=utf-8');
            echo Result::getWinPoss();
        });

$app->post('/postWinPoss',
        function  () use( $app)
        {
            $req = $app->request;
            $data = json_decode($app->request->getBody());
            require ('models/Result.php');
            Result::saveWinPoss($data);
        });

$app->run();
